package com.wxtl.action;

import java.io.File;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.wxtl.action.base.BaseAction;
import com.wxtl.action.qo.PackQo;
import com.wxtl.entity.UpgradePack;
import com.wxtl.service.UpgradePackService;
import com.wxtl.util.MD5Util;
import com.wxtl.util.Page;

public class PackAction extends BaseAction {
	private UpgradePack upgradePack=new UpgradePack();
	private String delFlag;
	private String pushFlag;
	private String downpath;
	
	/* 用于查询 */
	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private PackQo packQo=new PackQo();
	
	public PackQo getPackQo() {
		return packQo;
	}

	public void setPackQo(PackQo packQo) {
		this.packQo = packQo;
	}

	public String getDownpath() {
		return downpath;
	}

	public void setDownpath(String downpath) {
		this.downpath = downpath;
	}

	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	public String getPushFlag() {
		return pushFlag;
	}

	public void setPushFlag(String pushFlag) {
		this.pushFlag = pushFlag;
	}

	private int[] ids;

	private UpgradePackService upgradePackService;
	
	public void setUpgradePackService(UpgradePackService upgradePackService) {
		this.upgradePackService = upgradePackService;
	}
	
	public UpgradePack getUpgradePack() {
		return upgradePack;
	}

	public void setUpgradePack(UpgradePack upgradePack) {
		this.upgradePack = upgradePack;
	}

	public int[] getIds() {
		return ids;
	}
	
	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public String add() throws IOException{
		//获取路径
		String path = ServletActionContext.getServletContext().getRealPath("/")+"upload/";
		//构造file
		File file = new File(path+downpath);
		upgradePack.setMd5Value(MD5Util.getMD5String(file));
		upgradePack.setDownpath(path+downpath);
		upgradePackService.add(upgradePack,delFlag);
		return SUCCESS;
	}
	
	public String update() throws IOException{
		String path = "";
		if(downpath.indexOf(":")!=-1){
			path = downpath;
		}else{
			path = ServletActionContext.getServletContext().getRealPath("/")+"upload/"+downpath;
		}
		//构造file
		File file = new File(path);
		upgradePack.setMd5Value(MD5Util.getMD5String(file));
		upgradePack.setDownpath(path);
		upgradePackService.update(upgradePack);
		return SUCCESS;
	}
	
	public String delete(){
		upgradePackService.delete(ids);
		return SUCCESS;
	}
	
	public String query(){
		Page pageData=upgradePackService.query(super.getDataGrid(),upgradePack);
		setPageData(pageData);
		return SUCCESS;
	}
	
	public String queryCount(){
		Page pageData=upgradePackService.queryCount(super.getDataGrid(),packQo);
		setPageData(pageData);
		return SUCCESS;
	}
	
	public String detail(){
		setUpgradePack(upgradePackService.getPackById(id));
		return SUCCESS;
	}
}
